package com.gking.centerSystem.controller;

import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.dto.RolePermissionListDto;
import com.gking.centerSystem.service.RolePermissions.RolePermissionsService;
import com.gking.centerSystem.service.SystemInfo.SystemInfoService;
import com.gking.centerSystem.utils.GetAuthInfoUtil;
import com.gking.centerSystem.utils.LimitAccessUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/rolePermissions")
public class RolePermissionsController {

    @Resource
    private RolePermissionsService rolePermissionsService;

    @Resource
    private SystemInfoService systemInfoService;

    /**
     * 获取角色权限详情
     *
     * @param roleId 角色id
     * @return s
     */
    @RequiresPermissions("CS:manager:get:rolePermissions")
    @GetMapping("/rolePermissionsDetail/{roleId}")
    public Result<RolePermissionListDto> getRolePermissionsDetail(@PathVariable String roleId) {
        return rolePermissionsService.getRolePermissionsDetail(roleId);
    }

    /**
     * 获取角色权限详情 (设置访问ip白名单)
     *
     * @param roleId 角色id
     * @return s
     */
    @RequiresPermissions("CS:manager:get:rolePermissions")
    @GetMapping("/limit/rolePermissionsDetail/{roleId}")
    public Result<RolePermissionListDto> getRolePermissionsDetail2(@PathVariable String roleId, HttpServletRequest request) {
        Result<List<String>> systemIpList = systemInfoService.getSystemIpList();
        boolean b = LimitAccessUtil.remoteSubAccess(GetAuthInfoUtil.getClientIp(request), systemIpList.getData());
        if (!b) return Result.error("无法访问！");

        return rolePermissionsService.getRolePermissionsDetail(roleId);
    }


    /**
     * 为角色批量设置权限
     *
     * @param roleId            角色id
     * @param type              类型 （refresh, add, remove）
     * @param permissionsIdList 权限id 列表
     * @return s
     */
    @RequiresPermissions("CS:manager:set:rolePermissions")
    @PostMapping("/setRolePermissions/{roleId}")
    public Result<String> setRolePermissions(@PathVariable String roleId, @RequestParam String type,
                                             @RequestBody List<String> permissionsIdList) {
        return rolePermissionsService.setRolePermissions(roleId, type, permissionsIdList);
    }

}
